package com.example.Nurse_Notifier;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.widget.TextView;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

public class XbeeDevice implements Serializable {

    public static final String STATUS_OFF = "Устройство выключено";
    public static final String STATUS_SCAN = "Устройство в режиме сканирования";
    public static final String STATUS_IDLE = "Устройство простаивает";
    public static final String STATUS_ALARM = "Устройство в режиме оповещения";
    private SimpleDateFormat hmsDateFormat = new SimpleDateFormat("HH:mm:ss");
    private String startTime;
    private String endTime;
    private String resTimeString;
    private double start;
    private double resultTime;

    private Activity activity;
    private byte[] mac;
    private int id;
    private int status;
    private byte[] data;
    private String statusText = "";
    private String mainText = "Устройство №";

    private Drawable imageDrawable;
    private int bgColorWhite = 0xffffffff;
    private int bgColorRed = 0xFFC74B46;
    private int textColorMainPrimary;
    private int textColorAlarm;
    private int textColorMainSecond;
    private int curTextColorPrimary;
    private int curTextColorSecond;
    private int curColor;

    public XbeeDevice(Activity activity, byte[] mac, int id, int status) {
        this.activity = activity;
        this.mac = mac;
        this.id = id;
        this.status = status;
        customizeView(status);
        mainText += id + 1;
        textColorMainPrimary = activity.getResources().getColor(R.color.main_text);
        textColorMainSecond = activity.getResources().getColor(R.color.second_text);
        textColorAlarm = activity.getResources().getColor(R.color.perfect_white);
        curTextColorPrimary = textColorMainPrimary;
        curTextColorSecond = textColorMainSecond;
    }

    public void customizeView(int status) {
        switch (status) {
            case UdpServer.DEVICE_IDLE: {
                imageDrawable = activity.getResources().getDrawable(R.drawable.icon_blue_scheme_idle);
                statusText = STATUS_IDLE;
                curColor = bgColorWhite;
                curTextColorPrimary = textColorMainPrimary;
                curTextColorSecond = textColorMainSecond;
                break;
            }

            case UdpServer.DEVICE_SCAN: {
                imageDrawable = activity.getResources().getDrawable(R.drawable.icon_blue_scheme_scan);
                statusText = STATUS_SCAN;
                curColor = bgColorWhite;
                curTextColorPrimary = textColorMainPrimary;
                curTextColorSecond = textColorMainSecond;
                break;
            }

            case UdpServer.DEVICE_ALARM: {
                imageDrawable = activity.getResources().getDrawable(R.drawable.icon_blue_scheme_alarm);
                statusText = STATUS_ALARM;
                curColor = bgColorRed;
                curTextColorPrimary = textColorAlarm;
                curTextColorSecond = textColorAlarm;
                break;
            }

            case UdpServer.DEVICE_OFF: {
                imageDrawable = activity.getResources().getDrawable(R.drawable.icon_blue_scheme_off);
                statusText = STATUS_OFF;
                curColor = bgColorWhite;
                curTextColorPrimary = textColorMainPrimary;
                curTextColorSecond = textColorMainSecond;
                break;
            }

            default:
                break;
        }
    }

    public void changeDeviceStatus(int status) {
        if (status != getStatus()) {
            setStatus(status);
            customizeView(status);
        }
    }

    public XbeeDevice(Activity activity, String mac, int id, int status) {
        this(activity, mac.getBytes(), id, status);
    }

    public void setTextViewText(TextView textView, String text) {
        textView.setText(text);
    }

    public String getTime(double millis) {
        int sec = (int) millis / 1000;
        int h = sec / 3600;
        int m = (sec % 3600) / 60;
        int s = (sec % 3600) % 60;
        return "" + h + ":" + m + ":" + s;
    }

    public void setStartTime() {
        startTime = hmsDateFormat.format(new Date());
        start = System.currentTimeMillis();
    }

    public static String getCurTime() {
        return new SimpleDateFormat("HH:mm:ss").format(new Date());
    }

    public void setEndTime() {
        endTime = hmsDateFormat.format(new Date());
        resultTime = System.currentTimeMillis() - start;
        resTimeString = getTime(resultTime);
    }

    public byte[] getMac() {
        return mac;
    }

    public String getStringMac() {
        return Arrays.toString(mac);
    }

    public void setMac(byte[] mac) {
        this.mac = mac;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public byte[] getData() {
        return data;
    }

    public void setData(byte[] data) {
        this.data = data;
    }

    public int getCurTextColorSecond() {
        return curTextColorSecond;
    }

    public int getCurTextColorPrimary() {
        return curTextColorPrimary;
    }

    public String getMainText() {
        return mainText;
    }

    public int getCurColor() {
        return curColor;
    }

    public String getStatusText() {
        return statusText;
    }

    public Drawable getIconDrawable() {
        return imageDrawable;
    }
}